<template>
  <div :class="hidden?'hide':''">
    <!-- <center-title v-if="category" :process-title="category" /> -->
    <!-- <van-icon v-if="config.isShowCloseBtn && listData.canDel" name="close" class="close-btn" @click="closeFn" /> -->
    <dynamic-form-part-item v-for="item in items" :key="item.key" :item="item instanceof Array?item[1]:item" :form-index="formIndex" @idCardNumberCallback="idCardNumberCallback(arguments)" @idCardORCCallback="idCardOcrCallback" @onChangeOtherRequired="onChangeOtherRequired" @onExpressionProc="onExpressionProc" />
    <div v-if="config.isShowCloseBtn && listData.canDel" class="delBlock">
      <van-button name="close" class="noborder" block @click="closeFn">- 删除</van-button>
    </div>
  </div>
</template>
<script>
import DynamicFormPartItem from '@/components/d-form/DynamicFormPartItem'
// mport CenterTitle from '@/components/common/CenterTitle'

export default {
  components: { DynamicFormPartItem },
  props: {
    formIndex: {
      type: Number,
      default: 0
    },
    hidden: {
      type: Boolean,
      default: false
    },
    category: {
      type: String,
      default: ''
    },
    listData: {
      type: Object,
      default () {
        return {}
      }
    },
    items: {
      type: [Object, Array],
      required: true
    },
    config: {
      type: Object,
      default () {
        return {
          isShowCloseBtn: false
        }
      }
    }
  },
  methods: {
    closeFn () {
      this.$emit('close', { fnName: 'close' })
    },
    idCardNumberCallback(args) {
      debugger
      Object.entries(args[0].field).forEach(([key, value]) => {
        console.log(`${key} ${value}`)
        Object.entries(this.items).forEach(([itKey, itValue]) => {
          if (itValue.key === value) {
            Object.entries(args[0].value).forEach(([kv, vv]) => {
              if (key === kv) {
                itValue.value = vv
                itValue.text = vv
              }
            })
          }
        })
      })

      /* args[0].field.forEach(obj => {
        Object.entries(obj).forEach(([key, value]) => {
          console.log(`${key} ${value}`)
        })
        console.log('-------------------')
      }) */
    },
    idCardOcrCallback(args) {
      debugger
      console.log(this.items)
      Object.entries(args.field).forEach(([key, value]) => {
        console.log(`${key} ${value}`)
        Object.entries(this.items).forEach(([itKey, itValue]) => {
          if (itValue.key === value) {
            Object.entries(args.value).forEach(([kv, vv]) => {
              if (key === kv) {
                itValue.value = vv
                itValue.text = vv
              }
            })
          }
        })
      })
    },
    onChangeOtherRequired(args) {
      Object.entries(this.items).forEach(([itKey, itValue]) => {
        if (itValue.key === args.key) {
          itValue.required = args.value
        }
      })
    },
    onExpressionProc(args) {
      // {"d":"changeRequired('A0111', false)", "m":[{"c":"'{A0111.value}'==='已婚'","t":"changeRequired('A0111', true)"}]}
      // var act = 'this.' + args.d
      debugger
      var that = this
      for (var i = 0; i < args.m.length; i++) {
        var str_c = args.m[i].c
        debugger
        for (var j = 0; j < that.items.length; j++) {
          var str1 = '{' + that.items[j].key + '}'
          if (str_c.indexOf(str1) !== -1) {
            str_c = str_c.replaceAll(str1, that.items[j].value)
            if (str_c.indexOf('{') === -1) break
          }
        }
        debugger
        if (eval(str_c)) {
          eval('this.' + args.m[i].t)
        } else {
          eval('this.' + args.d)
        }
      }
    },
    /**
     * 改变单个必须输入项， k=key, v=value
     */
    changeRequired(k, v) {
      Object.entries(this.items).forEach(([itKey, itValue]) => {
        if (itValue.key === k) {
          itValue.required = v
        }
      })
    },
    /**
     * 改变多个必须输入项, g=[key1,key1] , v=value
     */
    changeRequiredGroup(g, v) {
      g.forEach((k) => {
        Object.entries(this.items).forEach(([itKey, itValue]) => {
          if (itValue.key === k) {
            itValue.required = v
          }
        })
      })
    }
  }
}
</script>
 <style lang="scss" scoped>
 .delBlock {
   margin-top: 2px;
 }
 .noborder {
   border: 0;
 }
// @import '@/assets/styles/variables.scss';
// .form-wrap {
//   position: relative;
// }
// /deep/.close-btn {
//   position: absolute;
//   right: 4px;
//   top: 8px;
//   font-size: 30px;
//   color: $dangerColor;
// }
</style>
